package com.yueyang.sojourn.server.controller;


import com.yueyang.sojourn.server.common.consts.Constants;
import com.yueyang.sojourn.server.controller.model.JsonResult;
import com.yueyang.uc.model.UcLoginUserV1;
import com.yueyang.uc.model.UcUserV1;
import com.yueyang.uc.spi.LoginUserSpi;
import com.yueyang.uc.spi.UserSpi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Stephen.Shi
 * @version v1
 * @summary
 * @Copyright (c) 2019, yueyang Group All Rights Reserved.
 * @since 2020/1/31
 */
@Slf4j
public class BaseController {
    @Autowired
    private LoginUserSpi loginUserSpi;
    @Autowired
    private UserSpi userSpi;

    protected JsonResult ok() {
        return JsonResult.ok("ok");
    }

    protected JsonResult ok(String message) {
        return JsonResult.ok(message);
    }

    protected JsonResult ok(int code, String message) {
        return JsonResult.ok(code, message);
    }

    protected JsonResult fail(int code, String message) {
        return JsonResult.fail(code, message);
    }

    protected JsonResult fail(String message) {
        return JsonResult.fail(message);
    }

    protected UcLoginUserV1 getLoginUser() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String token = request.getHeader(Constants.LOGIN_TOKEN);
        UcLoginUserV1 ucLoginUserV1 = null;
        if (StringUtils.isNotEmpty(token)) {
            try {
                ucLoginUserV1 = loginUserSpi.userInfo(token);
            } catch (Exception e) {
                log.warn(e.getMessage(), e);
            }
        }

        return ucLoginUserV1;
    }

    protected Long getLoginUserId() {
        UcUserV1 ucUserV1 = getLoginUser();
        if (ucUserV1 != null) {
            return getLoginUser().getId();
        }
        return null;
    }


    protected Map<Long,UcUserV1> findMapByIds(List<Long> userIds){
        if(userIds == null || userIds.isEmpty()){
            return new HashMap<>();
        }

        List<UcUserV1> userV1s = findByIds(userIds);
        Map<Long,UcUserV1> userV1Map = new HashMap<>();
        userV1s.stream().forEach((u) -> {userV1Map.put(u.getId(),u);});
        return userV1Map;
    }

    protected List<UcUserV1> findByIds(List<Long> userIds){
        if(userIds == null || userIds.isEmpty()){
            return new ArrayList<>();
        }
        return userSpi.findByIds(StringUtils.join(userIds,","));
    }

    protected UcUserV1 findUserById(Long userId){
        if(userId == null){
            return null;
        }
        return userSpi.findById(userId);
    }
}
